package com.xh.bussiness.tiku.controller;

import com.xh.bussiness.tiku.bean.request.SaveQuestionTypeRelationRequest;
import com.xh.bussiness.tiku.model.QuestionType;
import com.xh.bussiness.tiku.model.QuestionTypeRelation;
import com.xh.bussiness.tiku.service.IQuestionTypeRelationService;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.xh.bussiness.jingyou.service.IJYQuestionService;
import com.xh.bussiness.tiku.bean.dto.SubjectQuestionTypeDto;
import com.xh.bussiness.tiku.bean.request.QuestionTypeSaveRequest;
import com.xh.bussiness.tiku.service.IQuestionTypeService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/2/27 11:03
 **/
@Api(tags="题库-试题接口")
@RestController
@RequestMapping("/tiku/questionType")
public class QuestionTypeController {
    @Autowired
    private IQuestionTypeService questionTypeService;
    @Autowired
    private IJYQuestionService quetionService;
    @Autowired
    private IQuestionTypeRelationService questionTypeRelationService;

    @PostMapping("/queryAllQuestionTypes")
    @ApiOperation(value="查询所有试题类型", notes = "查询所有试题类型")
    public AjaxResult queryAllQuestionTypes(){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",questionTypeService.queryAllQuestionType());
    }

    @PostMapping("/querySubjectQuestionTypes")
    @ApiOperation(value="查询科目下所有试题类型", notes = "查询科目下所有试题类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name="subjectId", value="科目ID", required = true, dataType = "String"),
    })
    public AjaxResult querySubjectQuestionTypes(String subjectId){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",questionTypeService.queryQuestionTypeBySubjectId(subjectId));
    }

    @PostMapping("/saveSubjectQuestionTypes")
    @ApiOperation(value="保存科目下所有试题类型", notes = "保存科目下所有试题类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name="bean", value="科目、试题类型信息", required = true, dataType = "QuestionTypeSaveRequest"),
    })
    public AjaxResult saveSubjectQuestionTypes(@RequestBody QuestionTypeSaveRequest bean){
        SubjectQuestionTypeDto result= questionTypeService.saveSubjectQuestionTypes(bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",result);
    }

    @PostMapping("/deleteSubjectQuestionTypes")
    @ApiOperation(value="保存科目下所有试题类型", notes = "保存科目下所有试题类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name="subjectId", value="科目ID", required = true, dataType = "String"),
            @ApiImplicitParam(name="questionTypeId", value="试题类型ID", required = true, dataType = "Integer"),
    })
    public AjaxResult deleteSubjectQuestionTypes(String subjectId,Integer questionTypeId){
        String msg= questionTypeService.deleteSubjectQuestionTypes(subjectId,questionTypeId);
        return new AjaxResult(msg==null?AjaxResultConstant.Result.SUCCESS:AjaxResultConstant.Result.ERROR,msg);
    }
    @PostMapping("/queryBySubject")
    @ApiOperation(value="根据学科查询题型", notes = "查询菁优网的题型")
    @ApiImplicitParams({
            @ApiImplicitParam(name="subject", value="学科", required = true, dataType = "String"),
    })
    public AjaxResult queryBySubject(String subject) throws Exception{
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,quetionService.queryQuestionType(subject));
    }


    @PostMapping("/queryTypeBySubject")
    @ApiOperation(value="根据学科查询题型")
    @ApiImplicitParams({
            @ApiImplicitParam(name="subject", value="学科", required = true, dataType = "String"),
    })
    public AjaxResult queryTypeBySubject(String subject) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,questionTypeRelationService.queryBySubject(subject));
    }


    @ApiOperation("题型关系设置")
    @RequestMapping("/batchSaveQuestionTypeRelation")
    public AjaxResult batchSaveQuestionTypeRelation(@RequestBody SaveQuestionTypeRelationRequest request) {
        questionTypeRelationService.batchSaveQuestionTypeRelation(request);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @ApiOperation("分页查询题型关系")
    @RequestMapping("/queryQuestionTypeRelationPage")
    public XhPage queryQuestionTypeRelationPage(XhPage page,String subjectCode,String jyQuestionTypeName) {
        return questionTypeRelationService.queryPage(page, subjectCode, jyQuestionTypeName);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @ApiOperation(value="题型关系批量删除", notes = "题型关系批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ids", value="d集合", required = true, dataType = "String[]"),
    })
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        questionTypeRelationService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @ApiOperation(value = "数据字典数据批量启用停用", notes = "数据字典数据批量启用停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "数据字典id集合", required = true, dataType = "String[]"),
            @ApiImplicitParam(name = "status", value = "状态(0启用，1停用)", required = true, dataType = "Integer")
    })
    @PostMapping("/batchChangeStatus")
    @Transactional
    public AjaxResult batchChangeStatus(String[] ids, Integer status) {
        questionTypeRelationService.batchChangeStatus(ids, status);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @RequestMapping("/queryAllBasicQuestionType")
    public AjaxResult queryAllBasicQuestionType() {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,questionTypeService.queryAll());
    }

    @RequestMapping("/modifyQuestionTypeRelation")
    public AjaxResult modifyQuestionTypeRelation(QuestionTypeRelation questionTypeRelation) {
        if (questionTypeRelation == null || StringUtils.isEmpty(questionTypeRelation.getId())) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        questionTypeRelationService.update(questionTypeRelation);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 给复合题小题查询题型
     * @author 赵文博
     * @date 2021/7/7 17:00
     * @return java.util.List<com.xh.bussiness.tiku.model.QuestionType>
     **/
    @GetMapping("/queryQuestionType")
    public AjaxResult queryQuestionType(){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,questionTypeService.queryById());
    }
}
